package com.sky.service.admin.impl;

import com.sky.constant.MessageConstant;
import com.sky.exception.DataException;
import com.sky.mapper.common.CourierMapper;
import com.sky.mapper.common.ShortUrlInfoMapper;
import com.sky.mapper.common.TradeOrderMapper;
import com.sky.pojo.Courier;
import com.sky.pojo.CourierTradeOrder;
import com.sky.properties.AlismsPorperties;
import com.sky.result.Result;
import com.sky.service.admin.CommonService;
import com.sky.utils.AliOssUtil;
import com.sky.utils.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class CommonServiceImpl  implements CommonService {

    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AlismsPorperties alismsPorperties;

    @Autowired
    private TradeOrderMapper tradeOrderMapper;

    @Autowired
    private CourierMapper courierMapper;

    @Autowired
    private ShortUrlInfoMapper shortUrlInfoMapper;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @Override
    public Result<String> upload(MultipartFile file) throws IOException {
        //解决文件重名的问题
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;

        String url = aliOssUtil.upload(file.getBytes(), fileName);
        return Result.success(url);
    }

    /**
     * 发送短信
     *
     * @param telephone
     * @return
     */
    @Override
    public Result<String> sendSms(String telephone) throws Exception {
        //1. 校验手机号码是否合法
        if(StringUtils.isEmpty(telephone)){
            throw new DataException(MessageConstant.DATA_ERROR);
        }

        //2. 生成4位的验证码
        String code = "";
        Random random = new Random();
        for (int i=0; i<4; i++){
            int c = random.nextInt(10); //[0,9]
            code = code + c;
        }

        //3. 把生成的验证码存储到redis中 并设置过期时间5分钟过期
        redisTemplate.opsForValue().set("login:code:" + telephone, code, 5, TimeUnit.MINUTES);

        //4. 调用工具类发送短信
       // AliSmsUtils.sendSms(alismsPorperties, telephone, code);

        //5. 返回结果-需要包含验证码
        return Result.success(code);
    }

    /**
     * 用户查看商家 骑手 自己的位置信息
     *
     * @param tradeNo
     * @param courierId
     * @return
     */
    @Override
    public Result<String> loadRouteInfo(String tradeNo, Long courierId) {
        //1. 校验参数是否为空
        if(null == tradeNo || null == courierId){
            throw new DataException(MessageConstant.DATA_ERROR);
        }

        //2. 通过运单编号查询运单的数据
        CourierTradeOrder tradeOrder = tradeOrderMapper.selectByTradeNo(tradeNo);
        if(null == tradeOrder){
            throw new DataException(MessageConstant.DATA_ERROR);
        }

        //3. 根据运单数据得到商家和用户的详细地址 然后通过高德地图转换位坐标
        String shopAddress = MapUtils.addressToLnglat(tradeOrder.getShopAddress());
        String customerAddress = MapUtils.addressToLnglat(tradeOrder.getCustomerAddress());

        //4. 通过骑手的id查询骑手的信息(骑手的坐标)
        Courier courier = courierMapper.findById(courierId);

        //5. 按照指定的格式拼接坐标：用户的坐标_商家的坐标_骑手的坐标
        String location = customerAddress + "_" + shopAddress +"_" + courier.getCurLocation();

        return Result.success(location);
    }

    /**
     * 通过code 查询完整的请求地址
     *
     * @param code
     * @return
     */
    @Override
    public String queryFullUriByCode(String code) {
        return shortUrlInfoMapper.queryFullUriByCode(code);
    }


}
